<?php
	for($i=1;$i<=$_SESSION["count"];$i++)
	{
		if(isset($_SESSION["sp_id"][$i])&&isset($_SESSION["soluong"][$i]))
		{
	$ma = $_SESSION["sp_id"][$i];
	$sql = "select * from sp where sp_id = '".$ma."'";
	$result = DataProvider::ExecuteQuery($sql);
	$row = mysql_fetch_array($result);
	$tongtien += ($row["sp_gia"] * $_SESSION["soluong"][$i]) + ($row["sp_sall"]* $_SESSION["soluong"][$i]);
	$tongsluong += $_SESSION["soluong"][$i];
	$tbgia = $tongtien/$tongsluong;
	$sql ='select * from sp where sp_id = "'.$_SESSION["sp_id"][$i].'" ';
	$resul1t = DataProvider::ExecuteQuery($sql);
	$dong = mysql_fetch_array($resul1t);
	$sql ='select * from user where username = "'.$_SESSION["User"].'" ';
	$resul2t = DataProvider::ExecuteQuery($sql);
	$row1 = mysql_fetch_array($resul2t);
	$khang = $row1["fullname"].' - Địa Chỉ: '.$row1["address"].' - Số ĐT: '.$row1["cellphone"];
	$soluong1 = $_SESSION["soluong"][$i];
	if(isset($_POST["submit"]))
		$tongsp = $_POST["product_name"]; //tổng sản phẩm
		$dongia =$_POST["product_price"]; //đơn giá trung bình
		$soluong =$_POST["product_quantity"]; //số lượng
		$tongtien =$_POST["total_amount"]; //tổng tiền
		$thongtin =$_POST["product_description"]; //thông tin người mua
		$user =$_SESSION["User"]; //username
		$sanpham = $tongsp.'-'.$dongia.'-'.$soluong.'-'.$tongtien;
		$strSQL = "SELECT * FROM user where user.username = '$user'";
		$result = DataProvider::ExecuteQuery($strSQL);
		while($dong = mysql_fetch_array($result))
		{
		$diachi = $dong["address"];
		$dthoai = $dong["cellphone"];
		$querya = "Select sp_count from sp where sp_id = ".$ma."";
		$count = DataProvider::ExecuteQuery($querya);
		while($dong1 = mysql_fetch_array($count))
		{
		$set = ($dong1['sp_count']+$soluong1);
		$query1 = "UPDATE sp SET sp_count = '$set' where sp_id = ".$ma."";
		DataProvider::ExecuteQuery($query1);
		}
		}
		}
	}
		$query = "INSERT INTO hd VALUES ('','$thongtin',n'$sanpham','$tongtien',n'$diachi','$dthoai','$ti','false')";
		DataProvider::ExecuteQuery($query);
		header("location:home.php?typ=product&act=process");
	
?>